//
//  MyMain.java
//  ics314-calendar
//
//  Created by ICS314 Team Mississippi  on 7/24/14.
//  Copyright (c) 2014 JinHao Chen, Christian Caluya,Kevin Fujikawa. All rights reserved.
//
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.TimeZone;

public class Main {

	private JFrame frame;
	private JTextField d_start;
	private JTextField d_end;
	private JTextField summary;
	private JLabel lblGeographicPosition;
	private JTextField location;
	private JLabel lblTimeZoneIdentifier;
	private JTextField t_start;
	private JTextField t_end;
	private JComboBox priority;
	private JLabel lblNewLabel_1;
	private JLabel lblEx;
	private JComboBox classification;
	private JComboBox TimeZoneList;
	private JComboBox repeatSelector;
	private JCheckBox chckbxDay[];
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Main window = new Main();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Main() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 500);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Classification :");
		lblNewLabel.setBounds(33, 26, 124, 27);
		frame.getContentPane().add(lblNewLabel);
		
		JLabel lblTime = new JLabel("Priority : ");
		lblTime.setBounds(33, 108, 82, 27);
		frame.getContentPane().add(lblTime);
		
		JLabel lblTime_1 = new JLabel("Start Date and Time :");
		lblTime_1.setBounds(33, 151, 156, 27);
		frame.getContentPane().add(lblTime_1);
		
		JLabel lblTime_2 = new JLabel("End Date and Time :");
		lblTime_2.setBounds(33, 190, 156, 27);
		frame.getContentPane().add(lblTime_2);
		
		d_start = new JTextField();
		d_start.setColumns(10);
		d_start.setBounds(201, 151, 103, 28);
		frame.getContentPane().add(d_start);
		
		d_end = new JTextField();
		d_end.setColumns(10);
		d_end.setBounds(201, 190, 103, 28);
		frame.getContentPane().add(d_end);
		
		JLabel lblSummary = new JLabel("Summary : ");
		lblSummary.setBounds(33, 404, 156, 27);
		frame.getContentPane().add(lblSummary);
		
		summary = new JTextField();
		summary.setColumns(10);
		summary.setBounds(201, 403, 228, 28);
		frame.getContentPane().add(summary);
		
		lblGeographicPosition = new JLabel("Location :");
		lblGeographicPosition.setBounds(33, 65, 156, 27);
		frame.getContentPane().add(lblGeographicPosition);
		
		location = new JTextField();
		location.setText("UHM");
		location.setColumns(10);
		location.setBounds(201, 66, 203, 28);
		frame.getContentPane().add(location);
		
		lblTimeZoneIdentifier = new JLabel("Time\tzone\tidentifier :");
		lblTimeZoneIdentifier.setBounds(33, 365, 156, 27);
		frame.getContentPane().add(lblTimeZoneIdentifier);
		
		classification = new JComboBox();
		classification.setModel(new DefaultComboBoxModel(new String[] {"Public", "Private", "Confidential"}));
		classification.setToolTipText("");
		classification.setBounds(201, 27, 134, 27);
		frame.getContentPane().add(classification);
		
		t_start = new JTextField();
		t_start.setColumns(10);
		t_start.setBounds(326, 150, 103, 28);
		frame.getContentPane().add(t_start);
		
		t_end = new JTextField();
		t_end.setColumns(10);
		t_end.setBounds(326, 189, 103, 28);
		frame.getContentPane().add(t_end);
		
		priority = new JComboBox();
		priority.setModel(new DefaultComboBoxModel(new String[] {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}));
		priority.setBounds(201, 109, 134, 27);
		frame.getContentPane().add(priority);
		
		lblNewLabel_1 = new JLabel("Ex: 07/13/2014");
		lblNewLabel_1.setBounds(201, 175, 103, 20);
		frame.getContentPane().add(lblNewLabel_1);
		
		lblEx = new JLabel("Ex: 15:24:31");
		lblEx.setBounds(336, 175, 103, 20);
		frame.getContentPane().add(lblEx);
		JButton btnNewButton = new JButton("Submit");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				String dt_start = d_start.getText().trim() + " " + t_start.getText().trim();
				String dt_end = d_end.getText().trim() + " " + t_end.getText().trim();
				String s = "";
				if (!Judgment.isDate(dt_start)) s = "The Start Date and Time is not valid\n";
				if (!Judgment.isDate(dt_end)) s += "The End Date and Time is not valid\n";
				if (summary.getText().equals("")) s += "The summary can't be empty\n";
				if (!s.equals("")) {
					JOptionPane.showMessageDialog(null,
							s,
							"ERROR",
							JOptionPane.ERROR_MESSAGE); 
					return;
				}
				if (Judgment.compareDate(dt_start, dt_end) > 0 ) s +="The Start date cannot greater than End date";
				if (!s.equals("")) {
					JOptionPane.showMessageDialog(null,
							s,
							"ERROR",
							JOptionPane.ERROR_MESSAGE); 
					return;
				}
				String repeatBy = "";
				if (repeatSelector.getSelectedItem().equals("Every Week")) {
					repeatBy = Judgment.convertRepeatByWeek(chckbxDay);
				}
				if (repeatSelector.getSelectedItem().equals("Every Year")) {
					repeatBy = Judgment.convertRepeatByYear(chckbxDay);
				}
				Producer.init(String.valueOf(classification.getSelectedItem()), 
						String.valueOf(priority.getSelectedItem()),
						String.valueOf(TimeZoneList.getSelectedItem()),
						String.valueOf(location.getText()),
						String.valueOf(repeatSelector.getSelectedItem()),
						repeatBy,
						Judgment.convertDateAndTime(d_start.getText().trim() , t_start.getText().trim()), 
						Judgment.convertDateAndTime(d_end.getText().trim() , t_end.getText().trim()), 
						summary.getText());
			}
		});
		btnNewButton.setBounds(161, 443, 117, 29);
		frame.getContentPane().add(btnNewButton);
		
		TimeZoneList = new JComboBox();
		TimeZoneList.setBounds(201, 364, 203, 27);
		TimeZoneList.setModel(new DefaultComboBoxModel(TimeZone.getAvailableIDs()));
		frame.getContentPane().add(TimeZoneList);
		
		repeatSelector = new JComboBox();
		repeatSelector.setModel(new DefaultComboBoxModel(new String[] {"None", "Every Day", "Every Week", "Every Month", "Every Year"}));
		repeatSelector.setToolTipText("");
		repeatSelector.setBounds(201, 230, 134, 29);
		
		//Create CheckBox Item for Exception Date
		chckbxDay = new JCheckBox[12];
		for (int i = 0 ; i < 12 ; i++) {
			chckbxDay[i] = new JCheckBox("N/A");	
			chckbxDay[i].setSelected(false);
			if (i>5) {
				chckbxDay[i].setBounds(65*(i-6)+20, 315, 65, 25);
			} else {
				chckbxDay[i].setBounds(65*(i)+20, 280, 80, 25);
			}
			chckbxDay[i].setEnabled(false);
			frame.getContentPane().add(chckbxDay[i]);
		}
		
		repeatSelector.addActionListener (new ActionListener () {
		    public void actionPerformed(ActionEvent e) {
		    		MainWindowsHelper.dynamicCreateExceptionDate(repeatSelector.getSelectedItem().toString(), chckbxDay, frame);
		    		frame.getRootPane().setDoubleBuffered(true);
				frame.repaint();
		    }
		});
		frame.getContentPane().add(repeatSelector);
		
		JLabel lblRepeat = new JLabel("Repeat : ");
		lblRepeat.setBounds(33, 229, 156, 27);
		frame.getContentPane().add(lblRepeat);
		

	}
}
